#include "stdio.h"
#include "stdlib.h"

typedef int DataType;
#define M 100
typedef struct QNode {
    DataType data;
    struct QNode *next;
} QNode, *QueuePtr;

typedef struct {
    QueuePtr front;
    QueuePtr rear;
} LinkQueue;

int InitQueue(LinkQueue *Q) {
    Q->front = Q->rear = malloc(sizeof(QNode));
    if (!Q->front) {
        return 1;
    }
    Q->front = NULL;
    Q->rear = NULL;
    return 0;
}

int DestroyQueue(LinkQueue *Q) {
    while(Q->front){ // 重用 rear 指针作为临时变量
        Q->rear=Q->front->next;
        free(Q->front);
        Q->front=Q->rear;
    }
    return 0;
}

int QueueEmpty(LinkQueue Q) {
    return Q.front == NULL;
}

int GetHeadQueue(LinkQueue Q, DataType *x) {
    if (Q.front == NULL) {
        return 1;
    }
    *x = Q.front->data;
    return 0;
}

int EnqueueQueue(LinkQueue *Q, DataType x) {
    QNode *p = malloc(sizeof(QNode));
    if (!p) {
        return 1;
    }
    p->data = x;
    p->next = Q->front;
    Q->front = p;
    return 0;
}

int DequeueQueue(LinkQueue *Q, DataType *x) {
    if (Q->front == NULL) {
        return 1;
    }
    *x = Q->front->data;
    Q->front = Q->front->next;
    return 0;
}























